(function($){
	var KEY = "jforest.pagination";
	
	var PageInfo = function(pageNo,pageTotal,total,limit){
		if(pageNo){this.pageNo = pageNo;}else{this.pageNo = 1;}
		if(pageTotal){this.pageTotal = pageTotal;}else{this.pageTotal = null;}
		if(total){this.total = total;}else{this.total = null;}
		if(limit){this.limit = limit;}else{this.limit = 10;}
	};
	
	var Type1 = function(){
		this._setInfo = function(pageInfo){
			var self = this;
			var str = '当前第'+pageInfo.pageNo+'页';
			if(pageInfo.pageTotal){
				str = str + ",共"+pageInfo.pageTotal+"页";
			}
			if(pageInfo.total){
				str = str +",总数据" + pageInfo.total+"条"; 
			}
			self.$html.find('.pager-info').html(str);
		}
		this._buildHtml = function(fater){
			var pageInfo = fater.pageInfo,$el = fater.$element,self = this;
			self.$html = $('<ul class="pager"><li class="pager-info"></li><li class="previous"><a href="javascript:void(0);">上一页</a></li><li class="next"><a href="javascript:void(0);">下一页</a></li></ul>');
			self.$html.find('.previous').data('pageNo',pageInfo.pageNo - 1).click(function(){
				if(!$(this).hasClass('disabled')){
					var pageNo = $(this).data('pageNo');
					//禁用按钮
					$(this).addClass('disabled');
					if(pageNo > 0){fater._btnCallback(pageNo);}
				}
			});
			self.$html.find('.next').data('pageNo',pageInfo.pageNo + 1).click(function(){
				if(!$(this).hasClass('disabled')){
					var pageNo = $(this).data('pageNo');
					//禁用按钮
					$(this).addClass('disabled');
					if(pageNo > 0){fater._btnCallback(pageNo);}
				}
			});
			if(pageInfo.pageNo == 1){
				self.$html.find('.previous').addClass('disabled');
			}
			if(pageInfo.pageTotal && pageInfo.pageNo >= pageInfo.pageTotal){
				self.$html.find('.next').addClass('disabled');
			}
			$el.append(self.$html);
			self._setInfo(pageInfo);
		};
		this.loaded = function(pageInfo){
			var self = this;
			if(pageInfo.pageNo > 1){
				self.$html.find('.previous').data('pageNo',pageInfo.pageNo - 1).removeClass('disabled');
			}else{
				self.$html.find('.previous').data('pageNo',1).addClass('disabled');
			}
			if(!pageInfo.pageTotal || pageInfo.pageNo < pageInfo.pageTotal){
				self.$html.find('.next').data('pageNo',pageInfo.pageNo + 1).removeClass('disabled');
			}else{
				self.$html.find('.next').data('pageNo',1).addClass('disabled');
			}
			self._setInfo(pageInfo);
		};
	};

	var Type2 = function(){
		this._buildHtml = function(fater){
			var pageInfo = fater.pageInfo,$el = fater.$element,self = this;
			if(!pageInfo.pageTotal || pageInfo.pageNo < pageInfo.pageTotal){
				self.$html = $('<button type="button" data-loading-text="加载中..." class="btn btn-lg btn-primary" style="width:100%;">更多</button>');
				self.$html.data('pageNo',pageInfo.pageNo).click(function(){
					$(this).button('loading');
					var pageNo = $(this).data('pageNo') + 1;
					fater._btnCallback(pageNo);		
				}).button();
				$el.append(self.$html);
			}
		};
		this.loaded = function(pageInfo){
			$(this.$html).data('pageNo',pageInfo.pageNo);
			if(pageInfo.pageTotal && pageInfo.pageTotal <= pageInfo.pageNo){
				this.$html.hide();
			}
			$(this.$html).button('reset');
		};
	};

	var Pagination = function(element,options){
		this.$element = $(element);
		this.ajax = $.extend({},{
			url:null,
			dataType:'json',
			data: function (pageNo) {
				return {
					pageNo:pageNo
				};
			},
			results: function (result, pageNo) {
				return result;
			}
		},options.ajax);
		this.pageInfo = new PageInfo(options.pageNo,options.pageTotal,options.total,options.limit);
		this.options = {
			type:options.type?options.type:1,
			btnCallback:options.btnCallback?options.btnCallback:function(pageNo){return true;}
		};
		if(this.options.type == 1){
			this.obj = new Type1();
		}else if(this.options.type == 2){
			this.obj = new Type2();
		}else{
			return;		
		}
		this.obj._buildHtml(this,this.$element,this.options.pageNo,this.options.pageTotal);
		
	};

	Pagination.prototype = {
		_btnCallback:function(pageNo){
			var self = this,options = this.options,pageInfo = this.pageInfo;
			self.currentPageNo = pageNo;
			var isLoad = self.ajax.url?true:false;
			if(options.btnCallback){
				var value = options.btnCallback.call(self,self.currentPageNo);
				if(isLoad == true && (!value || value == false)){
					isLoad = false;
				}
			}
			if(isLoad == true){
				$.post(self.ajax.url,self.ajax.data.call(self,self.currentPageNo),function(result){
					self._ajaxCallback(result);
				},self.ajax.dataType)
			}else{
				pageInfo.pageNo = self.currentPageNo;
				self.obj.loaded(pageInfo);
			}
		},
		_ajaxCallback:function(result){
			var self = this,options = this.options,pageInfo = this.pageInfo;
			if(result){
				if($.isArray(result)){
					pageInfo.pageNo = self.currentPageNo;
					if(result.length == 0){
						pageInfo.pageTotal = self.currentPageNo;
					}
				}else if(self.ajax.dataType == 'json'){
					if(result.pageNo){pageInfo.pageNo = result.pageNo;}else{pageInfo.pageNo = self.currentPageNo};
					if(result.pageTotal){pageInfo.pageTotal = result.pageTotal;}
					if(result.total){pageInfo.total = result.total;}
				}
				self.obj.loaded(pageInfo);
				self.ajax.results.call(self,result);
			}else{
				self.ajax.results.call(self,result);
			}
		},
		refurbish:function(pageNo){
			if(!pageNo){pageNo = this.pageInfo.pageNo;}
			this._btnCallback(pageNo);
		}
	};
	
	$.fn.jfPagin = function(){
		var args = Array.prototype.slice.call(arguments, 0);
		var value = undefined;
		this.each(function () {
			var $this = $(this);
			if (args.length === 0 || typeof(args[0]) === "object") {
				opts = args.length === 0 ? {} : $.extend({}, args[0]);
				$this.data(KEY,new Pagination(this,opts)); //创建控件
			}else if (typeof(args[0]) === "string"){
				var entity = $this.data(KEY);
                if(entity === undefined){return;}
                value = entity[args[0]].apply(entity, args.slice(1));
			}else{
				 throw 'Invalid arguments to '+KEY+' plugin: ' + args;
			}
	    });
	    return (value === undefined) ? this : value;
	};
	
	$.fn.jfPagin.Constructor = Pagination;
	
})(window.jQuery)